package com.aiju.cash.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aiju.cash.R;
import com.aiju.cash.ui.main.entity.Radio;
import com.aiju.cash.ui.main.entity.Select;
import com.aiju.cash.ui.main.entity.SelectDate;
import com.aiju.cash.ui.main.view.BlotterFilterGroupView;
import com.aiju.cash.ui.main.view.time.TimePopupHelper;
import com.aiju.support.mvp.BaseTitleActivity;
import com.aiju.utils.StringUtil;
import com.bigkoo.pickerview.TimePopupWindow;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.aiju.cash.ui.main.view.time.TimePopupHelper.PICK_TIME_END;
import static com.aiju.cash.ui.main.view.time.TimePopupHelper.PICK_TIME_FROM;

public class SelectDateActivity extends BaseTitleActivity {

    TimePopupHelper mPikcHelper;
    private BlotterFilterGroupView ksxz;//快速选择
    private TextView edi_start_time;
    private TextView edi_end_time;
    private LinearLayout mLinearLayout;
    private SelectDate selectDate = new SelectDate();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_date);
        setCenterTitle("选择时间");
        mAiJuTitleBarView.setRightTitle("确认");
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("自定义".equals(selectDate.getName())) {
                    if (checkIsNull()) {
                        Toast.makeText(SelectDateActivity.this, "自定义模式下请选择时间", Toast.LENGTH_LONG).show();
                        return;
                    }
                    int t = compare_date(selectDate.getStartDate(), selectDate.getEndDate());
                    if (t > 0) {
                        Toast.makeText(SelectDateActivity.this, "开始时间不能晚于结束时间", Toast.LENGTH_LONG).show();
                    } else {
                        saveDate();
                        Intent intent = new Intent();
                        Bundle b = new Bundle();
                        b.putSerializable("date", selectDate);
                        intent.putExtras(b);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else {
                    setData();
                    saveDate();
                    Intent intent = new Intent();
                    Bundle b = new Bundle();
                    b.putSerializable("date", selectDate);
                    intent.putExtras(b);
                    setResult(RESULT_OK, intent);
                    finish();
                }


            }
        });
        ksxz = (BlotterFilterGroupView) findViewById(R.id.ksxz);
        edi_start_time = (TextView) findViewById(R.id.edi_start_time);
        edi_end_time = (TextView) findViewById(R.id.edi_end_time);
        mLinearLayout = (LinearLayout) findViewById(R.id.custom_date_view);
        ksxz.setData("快速选择", getList1());
        ksxz.setListener(new BlotterFilterGroupView.RadioGroupListener() {
            @Override
            public void checkedText(String value) {
                selectDate.setName(value);
                if ("自定义".equals(value)) {
                    mLinearLayout.setVisibility(View.VISIBLE);
                } else {
                    mLinearLayout.setVisibility(View.GONE);
                }
            }
        });

        edi_start_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPickHelper(edi_start_time, PICK_TIME_FROM);
            }
        });
        edi_end_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPickHelper(edi_end_time, PICK_TIME_END);
            }
        });
        getData();
    }

    List<Radio> getList1() {
        List<Radio> list = new ArrayList<>();
        list.add(new Radio("近7日"));
        list.add(new Radio("今天"));
        list.add(new Radio("昨天"));
        list.add(new Radio("本周"));
        list.add(new Radio("本月"));
        list.add(new Radio("上月"));
        list.add(new Radio("自定义"));
        return list;
    }


    private void initPickHelper(final TextView edi, int tag) {

        mPikcHelper = new TimePopupHelper(this, edi, new Date(), TimePopupWindow.Type.YEAR_MONTH_DAY);
        mPikcHelper.setmInterface(new TimePopupHelper.helperInterface() {
            @Override
            public void cancel(int tag) {
                mPikcHelper = null;
            }

            @Override
            public void callBackForData(int tag, Date date, boolean isAm, String ampm) {
                getPickResult(tag, date);
                if (tag == PICK_TIME_FROM) {
                    selectDate.setStartDate(date);
                } else {
                    selectDate.setEndDate(date);
                }
                edi.setText(dateFormatYYYYmmdd(date));
                mPikcHelper = null;
            }
        });

        mPikcHelper.startPopou(tag);
    }

    private void getPickResult(int tag, Date date) {
        if (date == null)
            return;
    }

    public static int compare_date(Date DATE1, Date DATE2) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = DATE1;//df.parse(DATE1);
            Date dt2 = DATE2;// df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }


    public static String dateFormatYYYYmmdd(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private void saveDate() {
        selectDate.setStartTime(edi_start_time.getText().toString().trim());
        selectDate.setEndTime(edi_end_time.getText().toString().trim());
    }

    private boolean checkIsNull() {
        String start = edi_start_time.getText().toString().trim();
        String end = edi_end_time.getText().toString().trim();
        if (StringUtil.isBlank(start) || StringUtil.isBlank(end)) {
            return true;
        } else {
            return false;
        }
    }

    void getData() {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle extras = intent.getExtras();
            Select select = (Select) extras.getSerializable("dd");
            selectDate.setName(select.getDateDes());
            selectDate.setStartDate(select.getFirstDate());
            selectDate.setEndDate(select.getLastDate());
            ksxz.showView(select.getDateDes());
            if("自定义".equals(select.getDateDes())){
                mLinearLayout.setVisibility(View.VISIBLE);
                edi_start_time.setText(select.getCommitStartTime());
                edi_end_time.setText(select.getCommitEndTime());
            }
        }
    }

    void setData() {
        String name = selectDate.getName();
        Calendar lastDate = Calendar.getInstance();
        switch (name) {
            case "近7日":
                lastDate.roll(Calendar.DATE, -6);//日期回滚7天
                selectDate.setStartDate(lastDate.getTime());
                selectDate.setEndDate(new Date());
                break;
            case "今天":
                selectDate.setStartDate(new Date());
                selectDate.setEndDate(new Date());
                break;
            case "昨天":
                lastDate.roll(Calendar.DATE, -1);//日期回滚1天
                selectDate.setStartDate(lastDate.getTime());
                selectDate.setEndDate(lastDate.getTime());
                break;
            case "本周":
                lastDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                selectDate.setStartDate(lastDate.getTime());
                lastDate.roll(Calendar.DATE, 6);//日期回滚1天
                selectDate.setEndDate(lastDate.getTime());
                break;
            case "本月":
                Calendar c = Calendar.getInstance();
                c.add(Calendar.MONTH, 0);
                c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
                selectDate.setStartDate(c.getTime());

                Calendar ca = Calendar.getInstance();
                ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
                selectDate.setEndDate(ca.getTime());
                break;
            case "上月":
                Calendar c1 = Calendar.getInstance();
                c1.add(Calendar.MONTH, -1);
                c1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
                selectDate.setStartDate(c1.getTime());

                Calendar ca1 = Calendar.getInstance();
                ca1.add(Calendar.MONTH, -1);
                ca1.set(Calendar.DAY_OF_MONTH, ca1.getActualMaximum(Calendar.DAY_OF_MONTH));
                selectDate.setEndDate(ca1.getTime());
                break;
        }


    }


}
